/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ventas_sie_venxref_estadisticas.java
 *
 * Created on 14/03/2011, 07:28:49 PM
 */

package sie_invdis;

import javax.swing.JTable;

import java.util.Date;
import java.util.Calendar;
import java.text.*;

import java.util.*;
import java.sql.*;
import javax.swing.*;
/**
 *
 * @author Michelle
 */
public class ventas_sie_venxref_estadisticas extends javax.swing.JDialog {

     private JTable jtable_tiendas;
     private bd_conexion x = new bd_conexion() ;
       //se creaa la variable resultset
      private ResultSet rs = null;
      private java.util.Date fechahoy=new Date();

    /** Creates new form ventas_sie_venxref_estadisticas */
    public ventas_sie_venxref_estadisticas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocation(25,25);
        this.cargadatos();
    }
private void cargadatos(){

        String texto="",texto2="",texto3="";
        jtable_tiendas=variables_global.gettabla_tiendas();
        List<String> list = new ArrayList<String>();
         float totalno=0,totalpi=0,totalve=0;
         DecimalFormat miformat = new DecimalFormat();
         Calendar fechaini = Calendar.getInstance(); // obtiene la fecha actual
         fechaini.set(2010,7,9);//esfecifico una fecha

          int numerotiendas=this.jtable_tiendas.getRowCount();
            Object valtie = null;


       // x.estableceConexion();
         try{
           texto="select * from x1diarioventas_xmesresumen_meses() as (fechames text)";
           rs = x.sieuno_executediversosv2(texto,variables_global.getconexiongral());
           while (rs.next())
                  {
                      list.add(rs.getString(1)); //cargo el list con las diferentes fecha formato 12-2010
                  }//consulto la bd
            }
            catch(Exception e)
            {
                    System.out.println(e.getMessage());
            }

            texto2+="<table border=\"1\">";
            texto2+="<tr><th colspan=\"5\"><h2>Reporte: Estadisticas Ventas y promociones<h2></th></tr><tbody>";
            texto2+="<tr><td><h3>Mes</h3></td><td><h3>Concepto /  Tiendas </h3></td>";
            for(int ii=0;ii<numerotiendas;ii++)
                        {
                             texto2+="<td>"+jtable_tiendas.getValueAt(ii, 0).toString()+"</td>";
                        }
            texto2+="<td><h2>Totales</h2></td></tr>";
            Iterator iter = list.iterator();
            while (iter.hasNext())
                {
                    texto3=iter.next().toString();
                    System.out.println("veo si es 2011"+texto3.substring(3,7));
               if (texto3.substring(3, 7).equals("2011"))
                  {
                   totalno=0;
                   totalpi=0;
                  totalve=0;
                    texto2+="<tr><td><h3>"+texto3+"</h3></td><td>Notas<br>Piezas<br>Importe<br>prom piezas <br>prom x nota <br>precio prom x arti</td>";
                    for(int ii=0;ii<numerotiendas;ii++)
                        {
                             valtie = jtable_tiendas.getValueAt(ii, 2);
                            // System.out.println(valtie.toString()+":"+texto3);
                              //monederos x tienda

                               texto="select * from x1diarioventas_xmesresumen_detalle('"+valtie.toString()+"','"+texto3+"') as (fe text,nnota bigint,totpie real,totven real);";
                                try{
                                  rs = x.sieuno_executediversosv2(texto,variables_global.getconexiongral());
                                  texto2+="<td>";
                                    while (rs.next())
                                            {
                                                totalno+=rs.getFloat(2);
                                                totalpi+=rs.getFloat(3);
                                                totalve+=rs.getFloat(4);
                                                texto2+=miformat.format(rs.getInt(2))+"<br>"+miformat.format(rs.getFloat(3))+"<br>"+miformat.format(rs.getFloat(4))+"<br>"+miformat.format(rs.getInt(3)/rs.getFloat(2))+"<br>"+ miformat.format(rs.getFloat(4)/rs.getFloat(2)) + "<br>"+ miformat.format(rs.getFloat(4)/rs.getFloat(3));
                                            }//consulto la bd
                                  texto2+="</td>";
                                    }
                                    catch(Exception e)
                                        {
                                            System.out.println(e.getMessage());
                                        }

                        }//termina el for
                         texto2+="<td>"+miformat.format(totalno)+"<br>"+miformat.format(totalpi)+"<br>"+miformat.format(totalve)+"<br>"+miformat.format(totalpi/totalno) +"<br>"+miformat.format(totalve/totalno) + "<br>" +miformat.format(totalve/totalpi)+"</td>";
                        texto2+="</tr>";
                    
                  }
                }//termina de iteractuar la lista
            texto2+="</table>";

            texto2+="<table border=\"1\">";
            texto2+="<tr><th colspan=\"5\"><h2>Reporte: Estadisticas Ventas y promociones<h2></th></tr><tbody>";
            texto2+="<tr><td><h3>Mes</h3></td><td><h3>Concepto /  Tiendas </h3></td>";
            for(int ii=0;ii<numerotiendas;ii++)
                        {
                             texto2+="<td>"+jtable_tiendas.getValueAt(ii, 0).toString()+"</td>";
                        }
            texto2+="";
            iter = list.iterator();
            totalpi=0;
            totalve=0;
            while (iter.hasNext())
                {
                    texto3=iter.next().toString();

                  if (texto3.substring(3, 7).equals("2011"))
                  {
                  totalno=0;
                  totalpi=0;
                  totalve=0;
                    texto2+="<tr><td><h3>"+texto3+"</h3></td>";
                    for(int ii=0;ii<numerotiendas;ii++)
                        {
                             valtie = jtable_tiendas.getValueAt(ii, 2);
                            // System.out.println(valtie.toString()+":"+texto3);
                              //monederos x tienda

                               texto="select * from x1ventasxrefresum_desc_descuentos('"+valtie.toString()+"','"+texto3+"') as (fe text,id_cat integer,cant_pro real,import_sindes real,impor_des double precision,impor_net double precision,descrip text)";
                                try{
                                  rs = x.sieuno_executediversosv2(texto,variables_global.getconexiongral());
                                  texto2+="<td>";
                                    while (rs.next())
                                            {
                                                totalpi+=rs.getFloat(3);
                                                totalve+=rs.getFloat(6);
                                                texto2+=rs.getString(7)+" PZ="+miformat.format(rs.getInt(3))+" $"+miformat.format(rs.getFloat(6))+"<br><br>";
                                            }//consulto la bd
                                  texto2+="</td>";
                                    }
                                    catch(Exception e)
                                        {
                                            System.out.println(e.getMessage());
                                        }

                        }//termina el for
                        texto2+="";
                        texto2+="</tr>";
                }//fin de la comparacion si es igual a 2011
             }//fin de iteractuar
                texto2+="</table>";


                this.jep_venxref_estadisticas.setText(texto2);
           //x.cierraConexion();
        
    }
   private  void ayuda()
            {
                String punto1="1.-Este Reporte muestra Estadisticas de Venta,cada una de las Opciones se autodescribe brevemente\n";
                String punto2="2.- Cada Boton tiene Una letra Resaltada el cual indica su metodo abreviado, uselo si desea mayor rapides ( ejemplo Alt+a obtendra ayuda automaticamente)\n";
                String punto3="3.- Los meses estan representados por el numero seguido del año. \n ";

                JOptionPane.showMessageDialog(null,""+punto1+punto2+punto3+"", "Sistema Integral(Estadisticas de venta) : Ayuda",1);
            }

     private void crearcomentario()
          {
                 String retorno="";

        String comentario=JOptionPane.showInputDialog("Ingresa Tu Sugerencia o Comentario:");
        if (comentario.length()>0)
        {
            rs = x.sieuno_executediversosv2("select * from x1distribucion_bitacorainserta('"+comentario+"', 'Ventas x ref estadistica');",variables_global.getconexiongral());
            try
            {

               while (rs.next())
                    {
                         retorno=rs.getString(1);
                   }
             }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
            if (retorno.equals("1"))
              {
                JOptionPane.showMessageDialog(null,"Gracias Tu comentario ha sido guardado", "Sistema Integral Comentarios",1);

              }

      }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jep_venxref_estadisticas = new javax.swing.JEditorPane();
        jPanel2 = new javax.swing.JPanel();
        jbtn_salir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jbtn_ayuda = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jmni_salir = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jmni_escape = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jmni_help = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sie_invdis.Sie_invdisApp.class).getContext().getResourceMap(ventas_sie_venxref_estadisticas.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jep_venxref_estadisticas.setContentType(resourceMap.getString("jep_venxref_estadisticas.contentType")); // NOI18N
        jep_venxref_estadisticas.setEditable(false);
        jep_venxref_estadisticas.setName("jep_venxref_estadisticas"); // NOI18N
        jScrollPane1.setViewportView(jep_venxref_estadisticas);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 764, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        jbtn_salir.setIcon(resourceMap.getIcon("jbtn_salir.icon")); // NOI18N
        jbtn_salir.setText(resourceMap.getString("jbtn_salir.text")); // NOI18N
        jbtn_salir.setName("jbtn_salir"); // NOI18N
        jbtn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_salirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(681, Short.MAX_VALUE)
                .addComponent(jbtn_salir)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jbtn_salir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setName("jPanel3"); // NOI18N

        jbtn_ayuda.setIcon(resourceMap.getIcon("jbtn_ayuda.icon")); // NOI18N
        jbtn_ayuda.setText(resourceMap.getString("jbtn_ayuda.text")); // NOI18N
        jbtn_ayuda.setName("jbtn_ayuda"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(673, Short.MAX_VALUE)
                .addComponent(jbtn_ayuda)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jbtn_ayuda)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jmni_salir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.ALT_MASK));
        jmni_salir.setIcon(resourceMap.getIcon("jmni_salir.icon")); // NOI18N
        jmni_salir.setText(resourceMap.getString("jmni_salir.text")); // NOI18N
        jmni_salir.setName("jmni_salir"); // NOI18N
        jmni_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmni_salirActionPerformed(evt);
            }
        });
        jMenu1.add(jmni_salir);

        jMenuBar1.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N

        jmni_escape.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        jmni_escape.setText(resourceMap.getString("jmni_escape.text")); // NOI18N
        jmni_escape.setName("jmni_escape"); // NOI18N
        jmni_escape.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmni_escapeActionPerformed(evt);
            }
        });
        jMenu2.add(jmni_escape);

        jMenuBar1.add(jMenu2);

        jMenu3.setText(resourceMap.getString("jMenu3.text")); // NOI18N
        jMenu3.setName("jMenu3"); // NOI18N

        jmni_help.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
        jmni_help.setIcon(resourceMap.getIcon("jmni_help.icon")); // NOI18N
        jmni_help.setText(resourceMap.getString("jmni_help.text")); // NOI18N
        jmni_help.setName("jmni_help"); // NOI18N
        jMenu3.add(jmni_help);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmni_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmni_salirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jmni_salirActionPerformed

    private void jbtn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_salirActionPerformed
        // TODO add your handling code here:
        this.jmni_salirActionPerformed(evt);
        
    }//GEN-LAST:event_jbtn_salirActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        this.crearcomentario();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jmni_escapeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmni_escapeActionPerformed
        // TODO add your handling code here:
        this.jmni_salirActionPerformed(evt);
    }//GEN-LAST:event_jmni_escapeActionPerformed

  
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ventas_sie_venxref_estadisticas dialog = new ventas_sie_venxref_estadisticas(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtn_ayuda;
    private javax.swing.JButton jbtn_salir;
    private javax.swing.JEditorPane jep_venxref_estadisticas;
    private javax.swing.JMenuItem jmni_escape;
    private javax.swing.JMenuItem jmni_help;
    private javax.swing.JMenuItem jmni_salir;
    // End of variables declaration//GEN-END:variables

}
